package com.lzs.bootsafety.business.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzs.bootsafety.business.logs.entity.SysOperLogs;
import com.lzs.bootsafety.business.sys.entity.SysRole;
import com.lzs.bootsafety.business.sys.entity.SysRoleMenu;
import com.lzs.bootsafety.business.sys.mapper.SysRoleMapper;
import com.lzs.bootsafety.business.sys.mapper.SysRoleMenuMapper;
import com.lzs.bootsafety.business.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysRole> getRoleByUserId(Long userId) {
        return roleMapper.selectRoleByUserId(userId);
    }

    @Override
    public List<SysRole> getRoleList() {
        return roleMapper.selectRoleList();
    }

    @Override
    public PageInfo<SysRole> getRoleByPage(SysRole sysRole) {
        Integer page = sysRole.getPage();
        Integer limit = sysRole.getLimit();
        PageHelper.startPage(page = page == null ? 1 : page,limit = limit == null ? 10 : limit);
        List<SysRole> sysRoleList = roleMapper.selectRoleByPage(sysRole);
        return new PageInfo<SysRole>(sysRoleList);
    }

    @Override
    public List<Long> getRolesByUserId(Long userId) {
        return roleMapper.selectRolesByUserId(userId);
    }

    /**
     * 添加角色
     * @param sysRole
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int addRole(SysRole sysRole) {
        int r = roleMapper.insertSelective(sysRole);
        addRoleMenu(sysRole);
        return r;
    }

    @Override
    public SysRole getRoleByRoleId(Long roleId) {
        return roleMapper.selectRoleByRoleId(roleId);
    }

    /**
     * 批量删除
     * @param roleIds
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int batchDelByPrimaryKey(Long[] roleIds) {
        System.out.println(roleIds[0]);
        sysRoleMenuMapper.batchRemoveRoleMenuByRoleId(roleIds);
        return roleMapper.batchDeleteByPrimaryKey(roleIds);
    }

    /**
     * 修改
     * @param sysRole
     * @return
     */
    @Override
    public int modifyByPrimaryKeySelective(SysRole sysRole) {
        Long[] roleIds = new Long[1];
        roleIds[0] = sysRole.getRoleId();
        sysRoleMenuMapper.batchRemoveRoleMenuByRoleId(roleIds);
        addRoleMenu(sysRole);
        return roleMapper.updateByPrimaryKeySelective(sysRole);
    }

    /**
     * 添加角色权限菜单
     * @param sysRole
     */
    public void addRoleMenu(SysRole sysRole){
        Long[] menuIds = sysRole.getMenuIds();
        if (menuIds.length > 0){
            List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
            for (Long menuId:menuIds){
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenus.add(sysRoleMenu);
            }
            if(sysRoleMenus.size() > 0){
                sysRoleMenuMapper.batchInsertRoleMenu(sysRoleMenus);
            }
        }

    }


}
